package com.ym.controller.patient.findDoctor;

import com.github.pagehelper.PageInfo;
import com.ym.common.constant.Constants;
import com.ym.core.annotation.ApiSign;
import com.ym.core.annotation.Log;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.shiro.ShiroUtil;
import com.ym.medical.domain.entity.SysDictEntity;
import com.ym.medical.domain.param.CounsellingRoomParam;
import com.ym.medical.domain.param.ExamDoctorParam;
import com.ym.medical.domain.param.InquiryOrderParam;
import com.ym.medical.domain.vo.CounsellingRoomRecordVO;
import com.ym.medical.domain.vo.DoctorPraisedVo;
import com.ym.medical.domain.vo.DoctorServiceItemsVo;
import com.ym.medical.domain.vo.HotConsultationVo;
import com.ym.medical.domain.vo.HotPointVo;
import com.ym.medical.domain.vo.PatientRateTagVo;
import com.ym.medical.domain.vo.ResultDocDetailVo;
import com.ym.medical.domain.vo.ResultDoctor;
import com.ym.medical.enums.InquiryOrderStateEnum;
import com.ym.medical.enums.OrderFlowRecordSatusEnum;
import com.ym.medical.service.ICommentRecordSV;
import com.ym.medical.service.ICommonSV;
import com.ym.medical.service.ICounsellingRoomSV;
import com.ym.medical.service.IInquiryOrderSV;
import com.ym.medical.service.IResultDoctorSV;
import com.ym.medical.service.ISysDictSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "患者端-找医生")
@RequestMapping(value = "/patient/find/doctor",headers= Constants.Api.VERSION)
@RestController
@ApiSign
public class FindDoctorController extends BaseController {
	private static final Logger LOGGER = LoggerFactory.getLogger(FindDoctorController.class);

	@Autowired
	private IResultDoctorSV resultDoctorService;
	@Autowired
	private ISysDictSV sysDictSV;
	@Autowired
	private IInquiryOrderSV inquiryOrderSV;
	@Autowired
	private ICounsellingRoomSV counsellingRoomService;
	@Autowired
	private ICommonSV commonSV;
	@Autowired
	private ICommentRecordSV commentRecordSV;

	@Log(dataBase = false, printBody = false)
	@ApiOperation("按条件搜索医生")
	@GetMapping(value = "/list")
	public APIResponse<PageInfo<ResultDoctor>> list(@RequestParam(defaultValue = "1", required = false) int pageNum,
													@RequestParam(defaultValue = "10", required = false) int pageSize,
													ExamDoctorParam examDoctor) {
		examDoctor.setCurrentId(ShiroUtil.getLoginId());
		PageInfo<ResultDoctor> list = resultDoctorService.listPage(pageNum, pageSize, examDoctor);
		return successData(list);
	}


	@ApiOperation("数据字典省份查询")
	@GetMapping("/selectDictProv")
	@ResponseBody
	public APIResponse<List<SysDictEntity>> selectDictProv() throws Exception {
		List<SysDictEntity> sysDicts = sysDictSV.selectDictProv();
		return successData(sysDicts);
	}

	@ApiOperation("数据字典省份查询")
	@GetMapping(value = "/selectByLevelAndCode", consumes = MediaType.ALL_VALUE)
	@ResponseBody
	public APIResponse<List<SysDictEntity>> selectByLevelAndCode(
			@ApiParam(name = "itemLevel", value = "查询级别 1省,2市,3县/区,4镇,5,村", required = true) int itemLevel,
			@ApiParam(name = "itemCode", value = "上级(省/市/县/镇)itemCode值", required = true) String itemCode) throws Exception {
		List<SysDictEntity> sysDicts = sysDictSV.selectByLevelAndCode(itemLevel, itemCode);
		return successData(sysDicts);
	}

	@Log
	@ApiOperation("问诊病情描述保存")
	@PostMapping(value = "/illness", consumes = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public APIResponse description( @RequestBody InquiryOrderParam inquiryOrder) {
		inquiryOrderSV.saveOrder(inquiryOrder);
		return success();
	}

	@Log
	@ApiOperation("医生个人信息-医生服务项")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "doctorId", value = "医生id", required = true, paramType = "query", dataType = "Long")
	})
	@ApiResponses({
			@ApiResponse(code = 200, message = "请求成功", response = DoctorServiceItemsVo.class),
	})
	@PostMapping(value = "/serviceItem", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	public APIResponse<DoctorServiceItemsVo> selectserviceItem(Long doctorId) {
		DoctorServiceItemsVo doctorServiceItemsVo = resultDoctorService.selectserviceItem(doctorId);
		return successData(doctorServiceItemsVo);
	}

	@Log
	@ApiOperation("图文咨询-查询定价规则")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "doctorId", value = "医生id", required = true, paramType = "query", dataType = "Long")
	})
	@PostMapping(value = "/price/rule", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	public APIResponse<ResultDocDetailVo> selectPriceRule(Long doctorId) {
		ResultDocDetailVo resultDocDetail = resultDoctorService.selectPriceRule(doctorId);
		return successData(resultDocDetail);
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation("医生答复列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "types", value = "类型:01未答复02已答复03已关闭04未总结05已总结.多个逗号分隔", required = false, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "doctorId", value = "医生id", required = true, paramType = "query", dataType = "Long")
	})
	@GetMapping(value = "/inquiry/list")
	public APIResponse<PageInfo<CounsellingRoomRecordVO>> inquiryList(
			@NotEmpty String types,
			@RequestParam(defaultValue = "1", required = false) int pageNum,
			@RequestParam(defaultValue = "5", required = false) int pageSize,
			Long doctorId
	) {
		CounsellingRoomParam model = new CounsellingRoomParam();
		model.setDoctorId(doctorId);
		model.setCourStates(types);
		model.setIoStates(InquiryOrderStateEnum.COMPLETE.getKey() + "," + InquiryOrderStateEnum.HAVE_ORDER.getKey());
		model.setFlowStatus(OrderFlowRecordSatusEnum.HAVE_BEEN_ABANDONED.getKey());
		PageInfo<CounsellingRoomRecordVO> counsellingRoomPageInfo = counsellingRoomService.listPagePatientVisitRecord(model, pageNum, pageSize);
		return successData(counsellingRoomPageInfo);
	}

	@Log
	@ApiOperation("患者端-医生（咨询人数，好评率，同行打分）")
	@PostMapping(value = "/evaluate", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	public APIResponse<DoctorPraisedVo> evaluate(Long doctorId) {
		DoctorPraisedVo doctorPraised = counsellingRoomService.evaluate(doctorId);
		return successData(doctorPraised);
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation(value = "医生-患者评价标签列表")
	@GetMapping(value = "/patientRateTag")
	@ResponseBody
	public APIResponse<List<PatientRateTagVo>> patientRateTag(Long doctId) {
		if (doctId == null) {
			doctId =ShiroUtil.getLoginId();
		}

		String sql = "SELECT * FROM (SELECT " +
				"b.id, " +
				"a.tag_type, " +
				"count(0) num, " +
				"b.tglb_name tglbName " +
				"from user_taglibrary a " +
				"inner join standar_taglibrary b on a.tag_id = b.id and a.tag_type = '01' " +
				"where taged_user_id = #{params.doctorId} and b.tglb_type = '01' " +
				"UNION ALL " +
				"SELECT " +
				"b.id, " +
				"a.tag_type, " +
				"count(0) num, " +
				"b.tglb_name tglbName " +
				"from user_taglibrary a " +
				"inner join custom_taglibrary b on a.tag_id = b.id and a.tag_type = '02' " +
				"where taged_user_id = #{params.doctorId} and b.tglb_type = '01'" +
				"  ) tab where tab.id is not null";
		Map<String, Object> params = new HashMap<>();
		params.put("doctorId", doctId);
		List<PatientRateTagVo> list = commonSV.queryListEntity(sql, params, PatientRateTagVo.class);
		return successData(list);
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation(value = "医生-患者评价")
	@GetMapping(value = "/patientRate")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "doctorId", value = "医生ID", required = false, paramType = "query"),
			@ApiImplicitParam(name = "okay", value = "满意度,1满意2一般3不满意", required = false, paramType = "query"),
			@ApiImplicitParam(name = "tagStand", value = "标准标签ID，多个", required = false, paramType = "query"),
			@ApiImplicitParam(name = "tagCustom", value = "自定义标签ID，多个", required = false, paramType = "query"),
	})
	@ResponseBody
	public APIResponse<List<HotPointVo>> patientRate(
			@RequestParam(value = "医生ID") Long doctId,
			@RequestParam(required = false) String okay,
			@RequestParam(required = false) Long[] tagStand,
			@RequestParam(required = false) Long[] tagCustom) {
		List<HotPointVo> commentRecordVos = commentRecordSV.selectCommentRecords(okay, doctId, tagStand, tagCustom);
		return successData(commentRecordVos);
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation("患者端-医生个人信息")
	@GetMapping(value = "/doctorId")
	public APIResponse<ResultDocDetailVo> selectByDoctorId(ExamDoctorParam examDoctor) {
		ResultDocDetailVo resultDocDetail = resultDoctorService.findDoctorById(examDoctor,ShiroUtil.getLoginId());
		return successData(resultDocDetail);
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation("患者端-医生个人信息-热度咨询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "doctorId", value = "医生的id", required = true, paramType = "query", dataType = "Long"),
			@ApiImplicitParam(name = "possDisease", value = "疾病标签", required = false, paramType = "query", dataType = "String")
	})
	@GetMapping(value = "/hotConsultation")
	public APIResponse<HotConsultationVo> hotConsultation(@RequestParam Long doctorId, String possDisease,
														  @RequestParam(defaultValue = "1", required = false) int pageNum,
														  @RequestParam(defaultValue = "5", required = false) int pageSize) {
		HotConsultationVo hotConsultation = resultDoctorService.hotConsultation(null, doctorId, possDisease, pageNum, pageSize);
		return successData(hotConsultation);
	}

	@Log
	@ApiOperation("患者端-医生个人信息-查询医生总结标签")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "doctorId", value = "医生的id", required = true, paramType = "query", dataType = "Long")
	})
	@PostMapping(value = "/conclusionLabel", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	public APIResponse<HotConsultationVo> conclusionLabel(Long doctorId, @RequestParam(defaultValue = "1", required = false) int pageNum,
														@RequestParam(defaultValue = "5", required = false) int pageSize) {
		HotConsultationVo hotConsultation = resultDoctorService.conclusionLabel(doctorId, pageNum, pageSize);
		return successData(hotConsultation);
	}
}
